package com.whxy02.tolearn.app.controller;

import com.whxy02.tolearn.app.beans.Card;
import com.whxy02.tolearn.app.beans.Document;
import com.whxy02.tolearn.app.beans.History;
import com.whxy02.tolearn.app.beans.User;
import com.whxy02.tolearn.app.resultObject.ResultState;
import com.whxy02.tolearn.app.resultObject.CardResultType;
import com.whxy02.tolearn.app.service.CardService;
import com.whxy02.tolearn.app.service.DocumentService;
import com.whxy02.tolearn.app.service.HistoryService;
import com.whxy02.tolearn.app.utils.DateUtils;
import com.whxy02.tolearn.app.utils.PackResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/card")
public class CardPage {
    @Resource
    HistoryService historyService;
    @Resource
    CardService cardService;
    @Resource
    DocumentService documentService;
    @Resource
    PackResult packResult;
    @Resource
    DateUtils dateUtils;


    /**
     * 获取打卡信息及打卡记录信息
     * @param request
     * @return
     */
    @RequestMapping("/document_data")
    public Map<String,Object> getDocument(HttpServletRequest request){

        User u= (User) request.getAttribute("user");
        /**
         *  页面返回结果类，封装了需要的各种信息数据
         */
        CardResultType result=new CardResultType();
        Integer userId=u.getUserId();
        try {

            Card card= cardService.getByUserId(userId);
            if(card!=null){


                List<Document> documents= documentService.getByUserId(userId);

                result.setDocuments(documents);
                result.setCardInfo(card);

                if(dateUtils.DateToStrng(documents.get(0).getCreateTime()).equals(result.getToday())){
                    return packResult.doPackToMap(ResultState.PUNCHED_CARD,result);
                }

                return packResult.doPackToMap(ResultState.NOT_PUNCHED_CARD,result);


            }
            else{
                return packResult.doPackToMap(ResultState.NEVER_PUNCH_CARD);
            }

        } catch (Exception e) {
            return packResult.doPackToMap(ResultState.SERVER_ERR,e);
        }

    }

    /**
     * 打卡
     * @param request
     * @param todayLog
     * @return
     */
    @RequestMapping("/punch_card")
    public Map<String,Object> punchCard(HttpServletRequest request,@RequestParam("todayLog")String todayLog){
        User user= (User) request.getAttribute("user");
        Integer userId=user.getUserId();
        try {
            List<History> histories= historyService.getByUserId(userId);
            if(histories.size()==0){
                return packResult.doPackToMap(ResultState.NOT_FINISH_TARGET);
            }
            if(!dateUtils.DateToStrng(histories.get(0).getCreateTime()).equals(dateUtils.DateToStrng(new Date()))){
                return packResult.doPackToMap(ResultState.NOT_FINISH_TARGET);
            }

            Card card= cardService.getByUserId(userId);
            Document d=new Document();
            if(card==null){

                card=new Card();
                card.setUserId(userId);
                card.setPoint(10);
                card.setSuccessionCount(1);
                card.setTotalCount(1);

                cardService.save(card);
                d.setTodayLog(todayLog);
                d.setUserId(userId);
                documentService.save(d);
                return packResult.doPackToMap(ResultState.FIRST_PUNCH_CARD);

            }else {
                List<Document> documents= documentService.getByUserId(userId);

                if(dateUtils.DateToStrng(documents.get(0).getCreateTime()).equals(dateUtils.getYesterday(new Date()))){
                    card.setSuccessionCount(card.getSuccessionCount()+1);
                }
                else if(dateUtils.DateToStrng(documents.get(0).getCreateTime()).equals(dateUtils.DateToStrng(new Date()))){
                    return packResult.doPackToMap(ResultState.PUNCHED_CARD);
                }
                else {
                    card.setSuccessionCount(1);
                }


                card.setTotalCount(card.getTotalCount()+1);
                card.setPoint(card.getSuccessionCount()*2+card.getPoint());
                cardService.change(card);

                d.setTodayLog(todayLog);
                d.setUserId(userId);
                documentService.save(d);
                return packResult.doPackToMap(ResultState.SUCCESS);
            }


        } catch (Exception e) {
            e.printStackTrace();
            return packResult.doPackToMap(ResultState.SERVER_ERR,e);
        }
    }


}
